//
//  main.cpp
//  KMP
//
//  Created by DeLong Yang on 2017/8/7.
//  Copyright © 2017年 YiXue. All rights reserved.
//  地址 ： http://blog.csdn.net/power721/article/details/6132380
//  参考的地址： http://www.cnblogs.com/c-cloud/p/3224788.html     // 

#include <iostream>

using namespace std;  // using name space

// 这是示范的代码 只用了一个循环
int match(const std::string &target,const std::string &pattern)
{
    unsigned long  target_length = target.size();
    unsigned long  pattern_length = pattern.size();
    
    int target_index = 0;
    int pattern_index = 0;
    while (target_index<target_length && pattern_index<pattern_length)
    {
        if (target[target_index] == pattern[pattern_index])
        {
            ++target_index;
            ++pattern_index;
        }
        else
        {
            target_index -= (pattern_index-1);
            pattern_index = 0;
        }
        
    }
    
    if (pattern_index == pattern_length)
    {
        return target_index - pattern_length;
    }
    
    return -1;
    
}

//  the last charcter can not compare
int main(int argc, const char * argv[]) {
    // insert code here...
//    std::cout << "Hello, World!\n";
    cout<<match("banananobano","nano")<<endl;
    return 0;
}
